package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    /**
     * 分页条件查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getList(WmSensitiveDto dto) {
        //1、参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2、业务处理
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper();
        //2.1、敏感词模糊查询
        lqw.like(dto.getName() != null, WmSensitive::getSensitives, dto.getName());
        //2.2、创建时间倒序
        lqw.orderByDesc(WmSensitive::getCreatedTime);
        //2.3、分页查询
        IPage iPage = new Page(dto.getPage(), dto.getSize());
        page(iPage, lqw);
        //3、封装查询结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        //3.1、补充参数
        result.setData(iPage.getRecords());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        //4、返回响应结果
        return result;
    }

    /**
     * 新增敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult add(WmSensitiveDto dto) {
        //1、参数校验
        if (dto == null || StringUtils.isEmpty(dto.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、业务处理
        if (sensitiveIsExit(dto)) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, dto.getSensitives() + "已经存在");
        //2.1、创建对象并完善属性
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        //2.2、保存
        save(wmSensitive);
        //2.3、存入redis
        saveToRedis();
        //、返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 存入redis

     */
    private void saveToRedis() {
        //先读取数据库得信息
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(null);
        // 只要其中得sensitive字段
        List<String> collect = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        // 存入redis
        redisTemplate.opsForValue().set("Sensitive",collect);
        List<String> sensitive = (List<String>) redisTemplate.opsForValue().get("Sensitive");
        System.out.println(sensitive);
    }

    /**
     * 判断敏感词是否存在
     *
     * @param dto
     * @return
     */
    private boolean sensitiveIsExit(WmSensitiveDto dto) {
        //判断敏感词是否已经存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        return sensitive != null;
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitiveDto dto) {
        //1、参数校验
        if (dto == null || dto.getId() == null || dto.getSensitives() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、业务处理
        //2.1、判断修改后是否重复
        if (sensitiveIsExit(dto))
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, dto.getSensitives() + "已存在，笨蛋");
        //2.2、先查出之前的
        WmSensitive wmSensitive = getById(dto.getId());
        //2.3、修改
        update(Wrappers.<WmSensitive>lambdaUpdate().eq(WmSensitive::getId, dto.getId()).set(WmSensitive::getSensitives, dto.getSensitives()));
        // 修改redis 新的换旧的
        saveToRedis();
        //3、返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //1、参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、业务处理 直接删除不带一丝感情
        //2.1、先查出对应的敏感词
        WmSensitive wmSensitive = getById(id);
        //2.2、删除
        removeById(id);
        //3、删除redis里的数据
        if (wmSensitive != null) {
            saveToRedis();
        }
        //3、返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
